//
//  MainViewController.m
//  XMPPHelper
//
//  Created by 蔡成汉 on 15/5/7.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "MainViewController.h"
#import "Message.h"
#import "XMPPHelper.h"
#import "AddFriendViewController.h"
#import "SendMsgViewController.h"
#import "RegisterViewController.h"
#import "CreatChatRoomViewController.h"

@interface MainViewController ()<UITextFieldDelegate>
{
    UITextField *userNameTextField;
    UITextField *userPasTextField;
    
    UIButton *loginButton;
}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"XMPPHelper";

    if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 100, 200, 30)];
    userNameTextField.delegate = self;
    userNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    userNameTextField.placeholder = @"用户名";
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userNameTextField.returnKeyType = UIReturnKeyNext;
    [userNameTextField becomeFirstResponder];
    userNameTextField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    [self.view addSubview:userNameTextField];
    
    userPasTextField = [[UITextField alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2 - 100, 150, 200, 30)];
    userPasTextField.delegate = self;
    userPasTextField.borderStyle = UITextBorderStyleRoundedRect;
    userPasTextField.placeholder = @"密码";
    userPasTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    userPasTextField.clearsOnBeginEditing = YES;
    userPasTextField.returnKeyType = UIReturnKeyDone;
    [self.view addSubview:userPasTextField];
    
    loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    loginButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 200, 200, 30);
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [loginButton addTarget:self action:@selector(loginButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];
    
    UIButton *logoutButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logoutButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 250, 200, 30);
    [logoutButton setTitle:@"登出" forState:UIControlStateNormal];
    [logoutButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal
     ];
    [logoutButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [logoutButton addTarget:self action:@selector(logoutButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logoutButton];
    
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addFriendButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 300, 200, 30);
    [addFriendButton setTitle:@"添加好友" forState:UIControlStateNormal];
    [addFriendButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [addFriendButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [addFriendButton addTarget:self action:@selector(addFriendButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addFriendButton];
    
    
    UIButton *sendMsgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sendMsgButton.frame = CGRectMake((self.view.frame.size.width/2 - 100), 350, 200, 30);
    [sendMsgButton setTitle:@"发送消息" forState:UIControlStateNormal];
    [sendMsgButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [sendMsgButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [sendMsgButton addTarget:self action:@selector(sendMsgButtonIsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendMsgButton];
    
    UIButton *creatChatGrop = [UIButton buttonWithType:UIButtonTypeCustom];
    creatChatGrop.frame = CGRectMake((self.view.frame.size.width/2 - 100), 400, 200, 30);
    [creatChatGrop setTitle:@"创建组群" forState:UIControlStateNormal];
    [creatChatGrop setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [creatChatGrop setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [creatChatGrop addTarget:self action:@selector(creatChatGropsTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:creatChatGrop];
    
    UIButton *getFriendsList = [UIButton buttonWithType:UIButtonTypeCustom];
    getFriendsList.frame = CGRectMake((self.view.frame.size.width/2 - 100), 450, 200, 30);
    [getFriendsList setTitle:@"获取好友列表" forState:UIControlStateNormal];
    [getFriendsList setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [getFriendsList setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [getFriendsList addTarget:self action:@selector(getFriendsListTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:getFriendsList];
    
    UIButton *registerUser = [UIButton buttonWithType:UIButtonTypeCustom];
    registerUser.frame = CGRectMake((self.view.frame.size.width/2 - 100), 500, 200, 30);
    [registerUser setTitle:@"注册新用户" forState:UIControlStateNormal];
    [registerUser setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [registerUser setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [registerUser addTarget:self action:@selector(registerUserTouch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:registerUser];
}

/**
 *  登陆
 *
 *  @param paramSender <#paramSender description#>
 */
-(void)loginButtonIsTouch:(UIButton *)paramSender
{
    if (userNameTextField.text != nil && userNameTextField.text.length>0 && userPasTextField.text != nil && userPasTextField.text.length>0)
    {
        XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
        [myXMPPHelper connectWithuserName:userNameTextField.text psd:userPasTextField.text.md5 result:^(BOOL connect, NSError *error) {
            if (connect == YES)
            {
                NSLog(@"登陆成功");
                [loginButton setTitle:@"登陆成功" forState:UIControlStateNormal];
                [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
                [loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
                [userNameTextField resignFirstResponder];
                [userPasTextField resignFirstResponder];
            }
            else
            {
                NSLog(@"登陆失败");
                [loginButton setTitle:@"登陆失败" forState:UIControlStateNormal];
                [loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
                [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
            }
        }];
    }
    else
    {
        UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"用户名或密码为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [myAlertView show];
    }
}

/**
 *  登出
 *
 *  @param paramSender 登出按钮
 */
-(void)logoutButtonIsTouch:(UIButton *)paramSender
{
    XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
    [myXMPPHelper disconnect];
}

/**
 *  添加好友
 *
 *  @param paramSender <#paramSender description#>
 */
-(void)addFriendButtonIsTouch:(UIButton *)paramSender
{
    AddFriendViewController *viewController = [[AddFriendViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}

/**
 *  发送消息
 *
 *  @param paramSender 发送消息按钮
 */
-(void)sendMsgButtonIsTouch:(UIButton *)paramSender
{
    SendMsgViewController *viewController = [[SendMsgViewController alloc]init];
    viewController.currentUserName = userNameTextField.text;
    [self.navigationController pushViewController:viewController animated:YES];
}


-(void)creatMessage
{
    AudioMessage *audioMessage = [[AudioMessage alloc]init];
    audioMessage.path = @"231254231";
    audioMessage.url = @"www.baidu.com";
    audioMessage.length = 5;
    NSString *audioMessageJSONString = [audioMessage jsonAudioMessage];
    NSLog(@"%@",audioMessageJSONString);
    
    ImageMessage *imageMessage = [[ImageMessage alloc]init];
    imageMessage.path = @"21332";
    imageMessage.url = @"234234";
    NSString *imageJSONString = [imageMessage jsonImageMessage];
    NSLog(@"%@",imageJSONString);
    
    LocationMessage *locationMessage = [[LocationMessage alloc]init];
    locationMessage.address = @"胡别";
    locationMessage.city = @"伤害";
    Location location;
    location.latitude = 123124;
    location.longitude = 1243;
    locationMessage.location = location;
    NSString *locationJSONString = [locationMessage jsonLocationMessage];
    NSLog(@"%@",locationJSONString);
    
    Message *message = [[Message alloc]init];
    message.fromId = @"234";
    message.toId = @"234325";
    message.content = @"123234123123";
    message.type = FriendsMessage;
    message.messageType = MessageTypeForText;
    message.sendTime = @"1234";
    
    NSString *messageJSONString = [message jsonMessage];
    NSLog(@"%@",messageJSONString);
}

-(void)creatChatGropsTouch:(UIButton *)paramSender
{
    CreatChatRoomViewController *viewController = [[CreatChatRoomViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}

/**
 *  获取好友列表
 *
 *  @param paramSender <#paramSender description#>
 */
-(void)getFriendsListTouch:(UIButton *)paramSender
{
    XMPPHelper *myXMPPHelper = [XMPPHelper myXMPPHelper];
    [myXMPPHelper getFriends];
}

-(void)registerUserTouch:(UIButton *)paramSender
{
    RegisterViewController *viewController = [[RegisterViewController alloc]init];
    [self.navigationController pushViewController:viewController animated:YES];
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == userNameTextField)
    {
        [textField resignFirstResponder];
        [userPasTextField becomeFirstResponder];
    }
    else
    {
        [textField resignFirstResponder];
    }
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
